package com.lxk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxk.pojo.Users;
import com.lxk.service.UsersService;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author JohnSoybean
 * @since 2020-07-14
 */
@RestController
@Validated
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * 获取用户基本信息
     * @return
     */
    @GetMapping("/info")
    public Result<Users> getUserInfo(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.select("username", "headimg", "phone", "sex", "wallet");
        Users users = usersService.getOne(wrapper);
        return Result.success(users);
    }

    /**
     * 修改头像
     * @param request
     * @param headimg
     * @return
     */
    @PostMapping("/modify/headimg")
    public Result<Integer> modifyHeadImage(HttpServletRequest request, @NotBlank(message = "不能为空！") String headimg) {
        Long userId = (Long) request.getAttribute("userId");
        Users users = new Users();
        users.setUserId(userId);
        users.setHeadimg(headimg);
        boolean flag = usersService.updateById(users);
        return flag ? Result.success(null) : Result.error(CodeMsg.USER_MODIFY_ERR);
    }

    /**
     * 修改昵称
     * @param request
     * @param username
     * @return
     */
    @PostMapping("/modify/username")
    public Result<Integer> modifyNickname(HttpServletRequest request, @NotBlank(message = "不能为空！") String username) {
        Long userId = (Long) request.getAttribute("userId");
        Users users = new Users();
        users.setUserId(userId);
        users.setUsername(username);
        boolean flag = usersService.updateById(users);
        return flag ? Result.success(null) : Result.error(CodeMsg.USER_MODIFY_ERR);
    }

    /**
     * 修改手机号
     * @param request
     * @param phone
     * @return
     */
    @PostMapping("/modify/phone")
    public Result<Integer> modifyPhone(HttpServletRequest request, @NotBlank(message = "不能为空！") String phone) {
        Long userId = (Long) request.getAttribute("userId");
        Users users = new Users();
        users.setUserId(userId);
        users.setPhone(phone);
        boolean flag = usersService.updateById(users);
        return flag ? Result.success(null) : Result.error(CodeMsg.USER_MODIFY_ERR);
    }

}

